package com.itheima.day3.stream;

import java.util.*;

/*
    收集：将元素收集入容器
        .collect(() -> c, (c, x) -> void, ?)

        () -> c             创建容器 c
        (c, x) -> void      将元素 x 加入 容器 c
 */
public class C11CollectTest {

    private String name;

    record Hero(String name, int strength) {

    }

    public static void main(String[] args) {
        List<String> collection = List.of("令狐冲", "风清扬", "独孤求败", "方证",
                "东方不败", "冲虚", "向问天", "任我行", "不戒", "不戒", "不戒", "不戒");

        // 1) 收集到 List
        List<String> result1 = collection.stream().collect(() -> new ArrayList<>(), (list, x) -> list.add(x), (a, b) -> {
        });
//        result1.forEach(System.out::println);

        /**
         * ArrayList::new   ()->new ArrayList()
         * ArrayList::add   (list,x)->list.add(x)
         */
        List<String> result2 = collection.stream().collect(ArrayList::new, ArrayList::add, (a, b) -> {
        });

        // 2) 收集到 Set
        Set<String> result3 = collection.stream().collect(LinkedHashSet::new, LinkedHashSet::add, (a, b) -> {
        });

        // 3)收集到 Map
        Map<String, Integer> result4 = collection.stream().collect(HashMap::new, (map, x) -> map.put(x, 1), (a, b) -> {
        });

        StringBuilder result5 = collection.stream().collect(StringBuilder::new, StringBuilder::append, (a, b) -> {
        });

        StringJoiner result6 = collection.stream().collect(() -> new StringJoiner(","), StringJoiner::add, (a, b) -> {
        });
    }
}
